package com.lecture.java8.lambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * @program: spring-lecture
 * @description:
 * @author: luyn
 * @create: 2020-03-27 15:29
 **/
public class TestLambda4 {
    // 随机生成num个随机数，并返回随机数的集合
    public List<Integer> getNumList(int num, Supplier<Integer> supplier) {
        List<Integer> numList = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            Integer iNum = supplier.get();
            numList.add(iNum);
        }
        return numList;
    }

    // 以特定的方式将字符串转换，并且将转化后的字符串返回。
    public String strHandler(String str, Function<String, String> function) {
        return function.apply(str);
    }

    // 将满足条件的字符串放入集合中。
    public List<String> filterStr(List<String> list, Predicate<String> predicate) {
        List<String> strList = new ArrayList<>();
        for (String str : list) {
            if(predicate.test(str)) {
                strList.add(str);
            }
        }
        return strList;
    }


    public static void main(String[] args) {
        int num = 10;
        TestLambda4 testLambda4 = new TestLambda4();
        List<Integer> numList = testLambda4.getNumList(num, () -> {
            return (int)(Math.random() * 100);
        });
        numList.forEach((item) -> System.out.println(item));

        String upperCaseStr = testLambda4.strHandler("Consumer", (item) -> item.toUpperCase());
        String lowerCaseStr = testLambda4.strHandler("Supplier", (item) -> item.toLowerCase());

        System.out.println("upperCaseStr：" + upperCaseStr + "||lowerCaseStr：" + lowerCaseStr);

        List<String> stringList = Arrays.asList("gg","a","fsdfsd","fsfwerwe","fsfsdfsd");
        List<String> filterStrList = testLambda4.filterStr(stringList, (str) -> {
            return str.length() > 2;
        });
        filterStrList.forEach(System.out::println);
    }
}
